package com.yida.gwj.expend.outside.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.domain.R;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.request.GwjExpendOutSideAllocatedAmountHouseRequest;
import com.yida.gwj.common.domain.GwjBaseBuild;
import com.yida.gwj.common.domain.GwjBaseCommunity;
import com.yida.gwj.common.domain.mq.MqSendResult;
import com.yida.gwj.common.domain.request.GwjBaseBuildRequest;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.handler.MqSendMessageHandler;
import com.yida.gwj.expend.outside.config.MaintenanceFundsBusinessConfiguration;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusiness;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessAttachment;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessBuild;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessHouse;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessLog;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountListDTO;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountSelectDTO;
import com.yida.gwj.expend.outside.domain.dto.ApplicationReportDTO;
import com.yida.gwj.expend.outside.domain.dto.ApplyBusinessCommunityDTO;
import com.yida.gwj.expend.outside.domain.dto.ChangeSchemeDTO;
import com.yida.gwj.expend.outside.domain.dto.DeveloperListDTO;
import com.yida.gwj.expend.outside.domain.dto.GetBusinessDetailDTO;
import com.yida.gwj.expend.outside.domain.dto.GwjHouseOwnerListDTO;
import com.yida.gwj.expend.outside.domain.dto.GwjMaintenanceFundsBusinessListDTO;
import com.yida.gwj.expend.outside.domain.dto.OwnerCheckProveDTO;
import com.yida.gwj.expend.outside.domain.dto.OwnerCommentsDTO;
import com.yida.gwj.expend.outside.domain.dto.StepConfigInfoDTO;
import com.yida.gwj.expend.outside.domain.message.MaintenanceFundsMqMessageBody;
import com.yida.gwj.expend.outside.domain.message.PreAcceptSubmitMessage;
import com.yida.gwj.expend.outside.domain.request.AllocatedAmountRequest;
import com.yida.gwj.expend.outside.domain.request.GwjHouseOwnerListRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessAddRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessListRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsCommunityListRequest;
import com.yida.gwj.expend.outside.domain.request.HousesUnderBuildRequest;
import com.yida.gwj.expend.outside.domain.request.ShareEquallyMoneyRequest;
import com.yida.gwj.expend.outside.enums.BusinessAttachmentServerTypeEnum;
import com.yida.gwj.expend.outside.enums.BusinessTypeEnum;
import com.yida.gwj.expend.outside.enums.FlagStatusEnum;
import com.yida.gwj.expend.outside.service.BusinessRequestGroup;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessAttachmentService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessBuildService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessService;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公共维修资金使用申请预受理业务
 *
 * @author lgp
 * @date 2023-02-20
 */
@Api(value = "公共维修资金使用申请预受理业务", tags = "公共维修资金使用申请预受理业务")
@RestController
@RequestMapping("/expend/outside")
public class GwjMaintenanceFundsBusinessController extends BaseController {

    @Autowired
    private MqSendMessageHandler mqSendMessageHandler;
    @Autowired
    private IGwjMaintenanceFundsBusinessService gwjMaintenanceFundsBusinessService;
    @Autowired
    private MaintenanceFundsBusinessConfiguration maintenanceFundsBusinessConfiguration;

    // 20230613
    @Autowired
    private IGwjMaintenanceFundsBusinessAttachmentService gwjMaintenanceFundsBusinessAttachmentService;

    // 20230615
    @Autowired
    private IGwjMaintenanceFundsBusinessBuildService gwjMaintenanceFundsBusinessBuildService;

    /**
     * 查询公维金使用申请业务列表
     */
    @ApiOperation(value = "查询公维金使用申请业务列表", response = GwjMaintenanceFundsBusinessListDTO.class)
    // @RequiresPermissions("gwj:expend:outside:list")
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/list")
    public TableDataInfo list(@Validated GwjMaintenanceFundsBusinessListRequest maintenanceFundsBusinessListRequest) {
        startPage();
        maintenanceFundsBusinessListRequest.setApplicantUserId(SecurityUtils.getLoginUser().getUserid());
        maintenanceFundsBusinessListRequest.setType(BusinessTypeEnum.PREPARE.getCode());
        List<GwjMaintenanceFundsBusinessListDTO> list = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusinessList(maintenanceFundsBusinessListRequest);
        return getDataTable(list);
    }

    /**
     * 获取公维金使用申请业务详细信息
     */
    @ApiOperation(value = "获取公维金使用申请业务详细信息", response = GetBusinessDetailDTO.class)
    @RequiresPermissions("gwj:expend:outside:info")
    @GetMapping(value = "/business/detail/{bizId}")
    public AjaxResult getInfo(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getDetailMessage(bizId));
    }

    /**
     * 业务信息-提交
     */
    @Log(title = "公维金支出外网[业务提交]", businessType = BusinessType.INSERT, systemName = "gwjOutside")
    @ApiOperation(value = "业务信息-提交")
    @RequiresPermissions("gwj:expend:outside:edit")
    @GetMapping(value = "/pre/accept/submit")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult preAcceptSubmit(@RequestParam("bizId") Long bizId) {
        PreAcceptSubmitMessage preAcceptSubmitMessage = gwjMaintenanceFundsBusinessService.preAcceptSubmit(bizId, ListUtil.toList(BusinessAttachmentServerTypeEnum.PROJECT_BUDGET, BusinessAttachmentServerTypeEnum.TRANSFORMATION_PLAN));
        String tag = maintenanceFundsBusinessConfiguration.tagProperties().getPreAcceptSubmit();
        MqSendResult mqSendResult = mqSendMessageHandler.syncSend(new MaintenanceFundsMqMessageBody() {
            {
                setJsonMsg(JSONUtil.toJsonStr(preAcceptSubmitMessage));
                setServerIdentification(preAcceptSubmitMessage.getMaintenanceFundsBusiness().getServerIdentification());
            }
        }, new MaintenanceFundsBusinessConfiguration.MaintenanceFundsBusinessSendRequest(tag, null));
        if (mqSendResult.isSuccess()) {
            return toAjax(FlagStatusEnum.SUC.getCode());
        } else {
            // throw new ServiceException("公维金资金申请提交数据到内网MQ异常");
            return AjaxResult.error("公维金资金申请提交数据到内网MQ异常");
        }
    }

    /**
     * 新增公维金使用资金申请业务
     */
    @Log(title = "公维金支出外网[申请业务]", businessType = BusinessType.INSERT, systemName = "gwjOutside")
    @ApiOperation(value = "新增公维金使用资金申请业务", response = Long.class)
    @RequiresPermissions("gwj:expend:outside:add")
    @PostMapping("/add/use/apply")
    public AjaxResult addUseApplyForm(@Validated @RequestBody GwjMaintenanceFundsBusinessAddRequest gwjMaintenanceFundsBusinessAddRequest) {
        gwjMaintenanceFundsBusinessAddRequest.setType(BusinessTypeEnum.PREPARE.getCode());
        gwjMaintenanceFundsBusinessAddRequest.setBusinessFlagType(GwjBusinessFlagType.EXPEND_OUTSIDE.getCode());
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.addMaintenanceFundsBusinessForm(gwjMaintenanceFundsBusinessAddRequest));
    }

    /**
     * 分摊金额列表
     */
    @ApiOperation(value = "分摊范围及金额-选中房屋列表", response = AllocatedAmountSelectDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/allocated/amount/list")
    public AjaxResult allocatedAmountList(@Validated @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.allocatedAmountList(allocatedAmountRequest, GwjBusinessFlagType.EXPEND_OUTSIDE));
    }

    /**
     * 小区业主信息管理 查询小区列表
     */
    @ApiOperation(value = "查询小区列表", response = GwjBaseCommunity.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/search/community")
    public TableDataInfo selectCommunityList(@Validated @RequestBody GwjMaintenanceFundsCommunityListRequest communityListRequest) {
        startPage();
        List<GwjBaseCommunity> list = gwjMaintenanceFundsBusinessService.selectCommunityList(communityListRequest);
        return getDataTable(list);
    }

    /**
     * 小区业主信息管理 查询业主及其房屋信息列表
     */
    @ApiOperation(value = "查询业主及其房屋信息列表", response = GwjHouseOwnerListDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/search/house/owner")
    public TableDataInfo selectHouseOwnerList(@Validated @RequestBody GwjHouseOwnerListRequest houseAndOwnerListRequest) {
        startPage();
        List<GwjHouseOwnerListDTO> list = gwjMaintenanceFundsBusinessService.selectHouseOwnerList(houseAndOwnerListRequest);
        return getDataTable(list);
    }

    /**
     * 按类型平摊金额
     */
    @Log(title = "公维金支出外网[分摊]", businessType = BusinessType.UPDATE, systemName = "gwjOutside")
    @ApiOperation(value = "分摊范围及金额-计算标准")
    @RequiresPermissions("gwj:expend:outside:edit")
    @PostMapping("/share/equally/money")
    public AjaxResult shareEquallyMoney(@RequestBody ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return toAjax(gwjMaintenanceFundsBusinessService.shareEquallyMoney(shareEquallyMoneyRequest));
    }

    /**
     * 修改平摊金额
     */
    @ApiOperation(value = "分摊范围及金额-修改平摊金额")
    @RequiresPermissions("gwj:expend:outside:edit")
    @PostMapping("/share/equally/money/update")
    public AjaxResult shareEquallyMoneyUpdate(@RequestBody ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return toAjax(gwjMaintenanceFundsBusinessService.shareEquallyMoneyUpdate(shareEquallyMoneyRequest));
    }

    /**
     * 分摊金额-选中房屋操作
     */
    @ApiOperation(value = "分摊范围及金额-选中房屋操作")
    @RequiresPermissions("gwj:expend:outside:edit")
    @PostMapping("/allocated/amount/arrange")
    public AjaxResult allocatedAmountArrange(@RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        return toAjax(gwjMaintenanceFundsBusinessService.allocatedAmountArrange(allocatedAmountRequest));
    }

    /**
     * 分摊金额-取消选中房屋操作
     */
    @ApiOperation(value = "分摊范围及金额-取消选中房屋操作")
    @RequiresPermissions("gwj:expend:outside:edit")
    @PostMapping("/allocated/amount/cancel")
    public AjaxResult allocatedAmountCancel(@RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        return toAjax(gwjMaintenanceFundsBusinessService.allocatedAmountCancel(allocatedAmountRequest));
    }

    /**
     * 保存公维金使用资金申请业务
     */
    @Log(title = "公维金支出外网[业务保存]", businessType = BusinessType.UPDATE, systemName = "gwjOutside")
    @ApiOperation(value = "更改公维金使用资金申请业务", response = int.class)
    @RequiresPermissions("gwj:expend:outside:edit")
    @PostMapping("/edit/use/apply")
    public AjaxResult editUseApplyForm(@Validated(BusinessRequestGroup.class) @RequestBody GwjMaintenanceFundsBusinessAddRequest gwjMaintenanceFundsBusinessAddRequest) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.editMaintenanceFundsBusinessForm(gwjMaintenanceFundsBusinessAddRequest));
    }

    @ApiOperation(value = "二次补件信息-查看申请报告", response = ApplicationReportDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/application/report")
    public AjaxResult getApplicationReport(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getApplicationReport(bizId));
    }

    @ApiOperation(value = "二次补件信息-查看业主征求意见", response = OwnerCommentsDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/owner/comments")
    public AjaxResult getOwnerComments(@RequestParam("bizId") Long bizId) {
        // startPage();
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getOwnerCommentsJSON(bizId));
    }

    @ApiOperation(value = "查看改造方案", response = ChangeSchemeDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/transformation/plan")
    public AjaxResult getTransformationPlan(@RequestParam("bizId") Long bizId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getTransformationPlan(bizId));
    }

    @ApiOperation(value = "二次补件信息-查看业主确认证明", response = OwnerCheckProveDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/owner/check/prove")
    public AjaxResult getOwnerCheckProve(@RequestParam("bizId") Long bizId) {
        // return AjaxResult.success(gwjMaintenanceFundsBusinessService.getOwnerCheckProve(bizId));
        // 20230628 hmh start
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getOwnerCheckProveIncludeVoteCount(bizId));
        // end
    }

    @Log(title = "公维金支出外网[二次补件提交]", businessType = BusinessType.UPDATE, systemName = "gwjOutside")
    @ApiOperation(value = "业务信息-二次补件提交")
    @GetMapping("/pre/accept/second/submit")
    public AjaxResult preAcceptSecondSubmit(@RequestParam("bizId") Long bizId) {
        // return toAjax(gwjMaintenanceFundsBusinessService.preAcceptSecondSubmit(bizId));
        // 20230721变更
        return toAjax(gwjMaintenanceFundsBusinessService.preAcceptSecondSubmitByBizId(bizId));

    }

    @ApiOperation(value = "使用申请业务查询基础数据小区信息", response = ApplyBusinessCommunityDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/community/{communityid}")
    public AjaxResult getCommunityInfo(@PathVariable("communityid") Long communityId) {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getCommunity(communityId));
    }

    @ApiOperation(value = "查询开发商列表", response = DeveloperListDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/developers")
    public TableDataInfo getDevelopers(@RequestParam(required = false) String companyName) {
        startPage();
        List<DeveloperListDTO> developers = gwjMaintenanceFundsBusinessService.getDevelopers(companyName);
        return getDataTable(developers);
    }

    @ApiOperation(value = "查询楼栋列表", response = GwjBaseBuild.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/get/builds")
    public TableDataInfo getBuilds(@RequestParam("communityId") Long communityId, @RequestParam(value = "buildName", required = false) String buildName) {
        startPage();
        List<GwjBaseBuild> builds = gwjMaintenanceFundsBusinessService.getBuilds(communityId, buildName);
        return getDataTable(builds);
    }

    // @ApiOperation(value = "查询使用申请基础信息", response = GwjBusinessBaseInfoDTO.class)
    // @RequiresPermissions("gwj:expend:outside:list")
    // @GetMapping("/get/user/apply/info")
    // public AjaxResult getUseApplyInfo() {
    // return AjaxResult.success(gwjMaintenanceFundsBusinessService.getUseApplyInfo());
    // }

    @ApiOperation(value = "分摊范围及金额-未选中房屋列表", response = GwjMaintenanceFundsBusinessHouse.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/allocated/amount/cancel/list")
    public TableDataInfo allocatedAmountCancelList(@Validated @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> maintenanceFundsBusinessHouseList = gwjMaintenanceFundsBusinessService.allocatedAmountCancelList(allocatedAmountRequest);
        return getDataTable(maintenanceFundsBusinessHouseList);
    }

    @ApiOperation(value = "获取步骤按钮权限信息", response = StepConfigInfoDTO.class)
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping(value = "/get/step/config/info")
    public AjaxResult getStepConfigInfo() {
        return AjaxResult.success(gwjMaintenanceFundsBusinessService.getStepConfigInfo(GwjBusinessFlagType.EXPEND_OUTSIDE));
    }

    @ApiOperation(value = "查询已选中楼栋列表")
    @RequiresPermissions("gwj:expend:outside:list")
    @GetMapping("/buildList/{bizId}")
    public TableDataInfo getSelectBuilds(@PathVariable("bizId") Long bizId) {
        // 20230715 取消分页，用于外网显示楼栋列表
        // startPage();
        List<GwjMaintenanceFundsBusinessBuild> selectBuilds = gwjMaintenanceFundsBusinessService.getSelectBuilds(bizId);
        return getDataTable(selectBuilds);
    }

    @ApiOperation(value = "查询楼栋下房屋列表")
    @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/houseList")
    public TableDataInfo getHouseList(@Validated @RequestBody HousesUnderBuildRequest housesUnderBuildRequest) {
        startPage();
        List<GwjMaintenanceFundsBusinessHouse> houseList = gwjMaintenanceFundsBusinessService.getHouseList(housesUnderBuildRequest);
        return getDataTable(houseList);
    }

    @Log(title = "公维金支出外网", businessType = BusinessType.EXPORT, systemName = "gwjOutside")
    @ApiOperation(value = "导出房屋数据")
    @RequiresPermissions("gwj:expend:outside:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody AllocatedAmountRequest allocatedAmountRequest) {
        List<AllocatedAmountListDTO> list = gwjMaintenanceFundsBusinessService.exportHouseList(allocatedAmountRequest);
        ExcelUtil<AllocatedAmountListDTO> util = new ExcelUtil<>(AllocatedAmountListDTO.class);
        util.exportExcel(response, list, "公维金维修资金业务房屋数据");
    }

    // ---------------------------新增功能区 hmh start --------------------------------------

    /**
     * 20230613
     * 
     * 根据业务编号和地区查询工程预算书
     * 
     * @param bizNum
     * @param areaId
     * @return
     */
    @InnerAuth
    @GetMapping("/expendImg/")
    // public R<List<GwjMaintenanceFundsBusinessAttachment>> expendImg(@RequestParam("bizNum") Long bizNum,
    // @RequestParam("areaId") Long areaId) {
    public R<JSONArray> expendImg(@RequestParam("bizNum") Long bizNum, @RequestParam("areaId") Long areaId) {
        // http://192.168.2.23:8081/api/gwj-expend-outside/expend/outside/attachment/list?serverIdentification=1668521799211610112&serverType=2
        GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setBizNum(bizNum);
                setAreaId(areaId);
                setType(1);// 默认查询预受理阶段
            }
        });
        check(gwjMaintenanceFundsBusiness);
        // 查询工程预算书图片
        List<GwjMaintenanceFundsBusinessAttachment> list = gwjMaintenanceFundsBusinessAttachmentService.selectGwjMaintenanceFundsBusinessAttachmentList(new GwjMaintenanceFundsBusinessAttachment() {
            /**
             *
             */
            private static final long serialVersionUID = -1292829370851359936L;

            {
                setServerType(2);// 工程预算书类型
                setServerIdentification(gwjMaintenanceFundsBusiness.getServerIdentification());
            }
        });
        if (StringUtils.isEmpty(list)) {
            return R.fail("工程预算书图片信息查询失败！");
        }

        return R.ok(JSONArray.from(list), "查询成功");
    }

    /**
     * 20230613
     * 
     * 根据业务编号和地区查询维修和更新、改造方案
     * 
     * @param bizNum
     * @param areaId
     * @return
     */
    @InnerAuth
    @GetMapping("/expendFile/")
    // public R<ChangeSchemeDTO> expendFile(@RequestParam("bizNum") Long bizNum, @RequestParam("areaId") Long areaId) {
    public R<JSONObject> expendFile(@RequestParam("bizNum") Long bizNum, @RequestParam("areaId") Long areaId) {
        GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setBizNum(bizNum);
                setAreaId(areaId);
                setType(1);// 默认查询预受理阶段
            }
        });
        check(gwjMaintenanceFundsBusiness);
        ChangeSchemeDTO changeSchemeDTO = gwjMaintenanceFundsBusinessService.getTransformationPlan(gwjMaintenanceFundsBusiness.getBizId());
        if (StringUtils.isNull(changeSchemeDTO)) {
            return R.fail("维修和更新、改造方案查询失败！");
        }
        return R.ok(JSONObject.from(changeSchemeDTO), "查询成功！");
    }

    /**
     * 20230613
     * 
     * 查询已选中房屋分摊范围及金额
     * 
     * @param bizNum 业务ID
     * @param areaId 地区ID
     * @param pageNum 页码
     * @param pageSize 页数
     * @param type 类型
     * @return
     */
    @InnerAuth
    @PostMapping("/expendBusinessHouse/")
    public R<JSONObject> expendBusinessHouse(@RequestBody GwjExpendOutSideAllocatedAmountHouseRequest gwjExpendOutSideAllocatedAmountHouseRequest) {
        // http: // 192.168.2.23:8081/api/gwj-expend-outside/expend/outside/allocated/amount/list?pageNum=1&pageSize=10
        if (StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getAreaId()) || StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getBizNum())) {
            return R.fail("请求参数中地区ID或业务编号不能为空！");
        }
        Long areaId = gwjExpendOutSideAllocatedAmountHouseRequest.getAreaId();
        GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setBizNum(gwjExpendOutSideAllocatedAmountHouseRequest.getBizNum());
                setAreaId(areaId);
                setType(StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getType()) ? 1 : gwjExpendOutSideAllocatedAmountHouseRequest.getType());// 默认查询预受理阶段[1]
            }
        });
        check(gwjMaintenanceFundsBusiness);
        PageUtils.startPage(gwjExpendOutSideAllocatedAmountHouseRequest.getPageNum(), gwjExpendOutSideAllocatedAmountHouseRequest.getPageSize());
        // 查询已选中
        AllocatedAmountSelectDTO aAllocatedAmountSelectDTO = gwjMaintenanceFundsBusinessService.allocatedAmountList(new AllocatedAmountRequest() {
            {
                setBizId(gwjMaintenanceFundsBusiness.getBizId());
                setAreaId(areaId);
                setRoomNum(gwjExpendOutSideAllocatedAmountHouseRequest.getRoomNum());// 房号
                setBuildCodes(gwjExpendOutSideAllocatedAmountHouseRequest.getBuildCodes());// 楼栋编码
                setUseType(gwjExpendOutSideAllocatedAmountHouseRequest.getUseType());// 规划用途
            }
        }, GwjBusinessFlagType.EXPEND_OUTSIDE);
        if (StringUtils.isNull(aAllocatedAmountSelectDTO)) {
            return R.fail("房屋信息查询失败！");
        }
        return R.ok(JSONObject.from(aAllocatedAmountSelectDTO), "查询成功！");
    }

    @InnerAuth
    @PostMapping("/expendBusinessHouse/unselected/")
    public R<JSONObject> expendBusinessHouseUnselected(@RequestBody GwjExpendOutSideAllocatedAmountHouseRequest gwjExpendOutSideAllocatedAmountHouseRequest) {
        if (StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getAreaId()) || StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getBizNum())) {
            return R.fail("请求参数中地区ID或业务编号不能为空！");
        }
        Long areaId = gwjExpendOutSideAllocatedAmountHouseRequest.getAreaId();
        GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setBizNum(gwjExpendOutSideAllocatedAmountHouseRequest.getBizNum());
                setAreaId(areaId);
                setType(StringUtils.isNull(gwjExpendOutSideAllocatedAmountHouseRequest.getType()) ? 1 : gwjExpendOutSideAllocatedAmountHouseRequest.getType());// 默认查询预受理阶段[1]

            }
        });
        check(gwjMaintenanceFundsBusiness);
        PageUtils.startPage(gwjExpendOutSideAllocatedAmountHouseRequest.getPageNum(), gwjExpendOutSideAllocatedAmountHouseRequest.getPageSize());
        // 查询未选中
        List<GwjMaintenanceFundsBusinessHouse> maintenanceFundsBusinessHouseList = gwjMaintenanceFundsBusinessService.allocatedAmountCancelList(new AllocatedAmountRequest() {
            {
                setBizId(gwjMaintenanceFundsBusiness.getBizId());
                setAreaId(areaId);
                setRoomNum(gwjExpendOutSideAllocatedAmountHouseRequest.getRoomNum());// 房号
                setBuildCodes(gwjExpendOutSideAllocatedAmountHouseRequest.getBuildCodes());// 楼栋编码
                setUseType(gwjExpendOutSideAllocatedAmountHouseRequest.getUseType());// 规划用途
            }
        });
        if (StringUtils.isNull(maintenanceFundsBusinessHouseList)) {
            return R.fail("房屋信息查询失败！");
        }
        // return R.ok(JSONArray.from(maintenanceFundsBusinessHouseList), "查询成功！");
        return R.ok(JSONObject.from(getDataTable(maintenanceFundsBusinessHouseList)), "查询成功！");

    }

    /**
     * 20230613
     * 
     * 查询业务楼栋信息
     * 
     * @param bizNum
     * @param areaId
     * @return
     */
    @InnerAuth
    @GetMapping("/expendBusinessBuilds/")
    // public R<AllocatedAmountSelectDTO> expendBusinessHouse(@RequestParam("bizNum") Long bizNum,
    // @RequestParam("areaId") Long areaId, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer
    // pageSize) {
    public R<JSONArray> expendBusinessBuilds(@RequestParam("bizNum") Long bizNum, @RequestParam("areaId") Long areaId) {
        // http: // 192.168.2.23:8081/api/gwj-expend-outside/expend/outside/allocated/amount/list?pageNum=1&pageSize=10
        GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness = gwjMaintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setBizNum(bizNum);
                setAreaId(areaId);
                setType(1);// 默认查询预受理阶段
            }
        });
        check(gwjMaintenanceFundsBusiness);
        // 查询已选中
        List<GwjMaintenanceFundsBusinessBuild> gwjMaintenanceFundsBusinessBuilds = gwjMaintenanceFundsBusinessBuildService.selectGwjMaintenanceFundsBusinessBuildByBusinessId(gwjMaintenanceFundsBusiness.getBizId());
        if (StringUtils.isNull(gwjMaintenanceFundsBusinessBuilds)) {
            return R.fail("楼栋信息查询失败！");
        }
        return R.ok(JSONArray.from(gwjMaintenanceFundsBusinessBuilds), "楼栋信息查询成功！");
    }

    private void check(GwjMaintenanceFundsBusiness gwjMaintenanceFundsBusiness) {
        if (StringUtils.isNull(gwjMaintenanceFundsBusiness)) {
            throw new ServiceException("未获取到对应业务数据信息，请核对后操作！");
        }
    }

    /**
     * 20230629
     * 
     * 获取过滤楼栋信息
     * 
     * @param buildRequest 楼栋请求数据
     * @return
     */
    // @RequiresPermissions("gwj:expend:outside:list")
    @PostMapping("/get/filte/build")
    public TableDataInfo getBuildsFilte(@RequestBody GwjBaseBuildRequest buildRequest) {
        startPage();
        List<GwjBaseBuild> builds = gwjMaintenanceFundsBusinessService.getBuildsFilte(buildRequest);
        return getDataTable(builds);
    }

    /**
     * 20230629
     * 
     * 外网业务日志查看
     * 
     * @param bizId 业务ID
     * @param result 业务状态[0:保存,1:提交,2:回退,3:送件,4:审核通过,5:完结,6:业务作废,7:取件,8:退件,]
     * @return
     */
    @GetMapping("/businessLog")
    public TableDataInfo selectBusinessLog(Long bizId, String result) {
        GwjMaintenanceFundsBusinessLog gwjMaintenanceFundsBusinessLog = new GwjMaintenanceFundsBusinessLog();
        gwjMaintenanceFundsBusinessLog.setBizId(bizId);
        gwjMaintenanceFundsBusinessLog.setResult(result);
        startPage();
        List<GwjMaintenanceFundsBusinessLog> gwjMaintenanceFundsBusinessLogList = gwjMaintenanceFundsBusinessService.selectGwjMaintenanceFundsBusinessLogList(gwjMaintenanceFundsBusinessLog);
        return getDataTable(gwjMaintenanceFundsBusinessLogList);
    }

    // 导入20230704
    @Log(title = "公维金支出外网", businessType = BusinessType.IMPORT, systemName = "gwjOutside")
    @RequiresPermissions("gwj:expend:outside:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, @RequestParam(name = "updateSupport") boolean updateSupport, @RequestParam(name = "bizId") Long bizId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空！");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        String result = gwjMaintenanceFundsBusinessService.importHouseList(file, updateSupport, bizId);
        return AjaxResult.success(result);
    }

    /**
     * 检测导入文件类型是否合规
     *
     * @param file
     * @return
     * @throws IOException
     * @throws IORuntimeException
     */
    private boolean checkImportFile(MultipartFile file) throws Exception {
        // 检查文件类型
        // String type = FileTypeUtil.getType(file.getInputStream());
        String type = FileTypeUtils.getExtension(file);
        // 限制的文件类型集合
        if (StringUtils.inStringIgnoreCase(type, "xlsx", "xls")) {
            return true;
        }
        return false;
    }

    /**
     * 步骤回调
     * 
     * @param jsonMsg 回调信息
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/resultNotify")
    @ApiOperation(value = "步骤回调", response = Boolean.class)
    public R<Boolean> resultNotify(@RequestParam("jsonMsg") @NotBlank(message = "回调信息不能为空") String jsonMsg) {
        return gwjMaintenanceFundsBusinessService.resultNotify(jsonMsg);
    }

    /**
     * 回退步骤回调
     * 
     * @param jsonMsg 回调信息
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/rollback/resultNotify")
    @ApiOperation(value = "步骤回调", response = Boolean.class)
    public R<Boolean> rollbackResultNotify(@RequestParam("jsonMsg") @NotBlank(message = "回调信息不能为空") String jsonMsg) {
        return gwjMaintenanceFundsBusinessService.rollbackResultNotify(jsonMsg);
    }

    /**
     * 预受理审核
     * 
     * @param jsonMsg 预受理审核参数
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/preAcceptExamine")
    @ApiOperation(value = "步骤回调", response = Boolean.class)
    public R<Boolean> preAcceptExamine(@RequestParam("jsonMsg") @NotBlank(message = "预受理审核信息不能为空") String jsonMsg) {
        return gwjMaintenanceFundsBusinessService.preAcceptExamine(jsonMsg);
    }

    /**
     * 业主证明内部调用
     * 
     * @param outServerIdentification 外网业务标识
     * @return
     */
    @InnerAuth
    @ApiOperation("业主证明内部调用")
    @GetMapping("/ownerCheckInner")
    public R<JSONObject> ownerCheckInner(@RequestParam("outServerIdentification") String outServerIdentification, @RequestParam("bizFlagId") Integer bizFlagId) {
        return gwjMaintenanceFundsBusinessService.ownerCheckInner(outServerIdentification, bizFlagId);
    }
    // ---------------------------新增功能区 end --------------------------------------
}
